package com.controller;
/*
* 专门用来处理用户业务逻辑的控制器
* */

import com.entity.Users;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")  //表示这里所有的动作都是处理用户业务逻辑请求的。
public class UsersController {


    @GetMapping(value="auth")
    public Users login(String username,String password){
        System.out.println("执行用户登录,查询用户信息....");
        System.out.println("用户名："+username+",密码："+password);
        Users loginUser = new Users(100,username,password);
        return loginUser;
    }


    @PostMapping(value="/")
    public Users add(@RequestBody Users user){
        System.out.println("执行用户注册或者添加新用户....");
        System.out.println("新增用户资料："+user);
        return user;
    }

    @GetMapping(value="/")
    public List<Users> queryAll(){
        System.out.println("查询所有用户....");
        List<Users> users = new ArrayList<Users>();
        users.add(new Users(100,"zhangsan","123456"));
        users.add(new Users(101,"lisi","654321"));
        users.add(new Users(102,"wangwu","555555"));
        return users;
    }

    @GetMapping(value="/{uid}")
    public Users queryAll(@PathVariable("uid") int uid){
        System.out.println("查询单个用户....");
        System.out.println("查询用户编号："+uid);
        Users user = new Users(uid,"张三","123456");
        return user;
    }

    @PutMapping(value="/")
    public Map<String,Object> update(@RequestBody Users user){
        System.out.println("更新单个用户....");
        System.out.println("更新用户资料："+user);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",200);
        result.put("msg","更新成功");
        return result;
    }

    @DeleteMapping(value="/{uid}")
    public Map<String,Object> delete(@PathVariable("uid") int uid){
        System.out.println("删除单个用户....");
        System.out.println("删除用户编号：" + uid);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("code",200);
        result.put("msg","删除成功");
        return result;
    }

}
